package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.impl.dao.BreedFactoryDao;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.enums.VeterinaryDrugsReportStatus;
import com.jiantong.work.impl.dao.VeterinaryDrugsReportDao;
import com.jiantong.work.model.VeterinaryDrugsReport;
import com.jiantong.work.service.VeterinaryDrugsReportReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 兽药记录
 * Created by mz03 on 2017/3/14.
 * @author :zyp
 */
@Slf4j
@Service
@RpcProvider
public class VeterinaryDrugsReportReadServiceImpl implements VeterinaryDrugsReportReadService{


    private final VeterinaryDrugsReportDao veterinaryDrugsReportDao;
    private final BreedFactoryDao breedFactoryDao;

    @Autowired
    public VeterinaryDrugsReportReadServiceImpl(VeterinaryDrugsReportDao veterinaryDrugsReportDao, BreedFactoryDao breedFactoryDao) {
        this.veterinaryDrugsReportDao = veterinaryDrugsReportDao;
        this.breedFactoryDao = breedFactoryDao;
    }

    /**
     * id 查找
     * @param id
     * @return veterinaryDrugsReport
     * Response<VeterinaryDrugsReport> finById(Long id);
     */
    @Override
    public Response<VeterinaryDrugsReport> finById(Long id) {
      try{
          //这个是借用Dao层的类实现work-api中接口里功能--根据id查询 把查询出来的结果用VeterinaryDrugsReport类型的对象接收.
          VeterinaryDrugsReport veterinaryDrugsReport = veterinaryDrugsReportDao.findById(id);
          if(veterinaryDrugsReport == null){
              log.error("veterinaryDrugsReport (id={}) is not found", id);
              return Response.fail("veterinaryDrugsReport.not.found");
          }
          return Response.ok(veterinaryDrugsReport);
      } catch (Exception e) {
          log.error("failed to find veterinaryDrugsReport(id={}),cause:{}",id,Throwables.getStackTraceAsString(e));
          return Response.fail("veterinaryDrugsReport.find.fail");
      }
    }

    @Override
    public Response<Map<String, Object>> findStatusById(Long id) {
        try{
            Integer status = veterinaryDrugsReportDao.findStatusById(id);
            Map<String , Object> map = Maps.newHashMap();
            if(status == null){
                log.error("veterinary Drugs Report status (id={}) is not found", id);
                return Response.fail("veterinary Drugs Report.status.not.found");
            }
            map.put("status",status);
            return Response.ok(map);
        }catch (Exception e){
            log.error("veterinary status (id={}) is not found", id);
            return Response.fail("veterinary.status.not.found");
        }
    }

    @Override
    public Response<Paging<VeterinaryDrugsReport>> pagingVeterinaryDrugsReport(Integer pageNo, Integer pageSize, Integer status,Long breedFactoryId) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();

            criteria.put("breedFactoryId",breedFactoryId);

            if (status !=null) criteria.put("status", status);

            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(veterinaryDrugsReportDao.paging(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find feedReport(id={}), cause:{}", status, Throwables.getStackTraceAsString(e));
            return Response.fail("feedReport.find.fail");
        }
    }

    @Override
    public Response<Paging<VeterinaryDrugsReport>> pagingVeterinaryDrugsReports(Integer pageNo, Integer pageSize, Long breedFactoryId, Integer breedType, Date fromDate, Date toDate) {

        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();

            List<Long> breedFactoryIds = new ArrayList<>();
            if(breedFactoryId!=null){
                breedFactoryIds.add(breedFactoryId);
            }else {
                LoginUser loginUser = UserUtil.getCurrentUser();
                breedFactoryIds = loginUser.getBreedFactoryIds();
            }
            //在这里根据breedType赛选一下养殖场,从breedFactoryIds中去掉不是这个养殖类型的养殖场ID
            if (breedType != null) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("breedType",breedType);
                if (!breedFactoryIds.isEmpty()) {
                    map.put("breedFactoryIds", breedFactoryIds);
                    breedFactoryIds = breedFactoryDao.selectedByType(map);
                }
            }

            if(!breedFactoryIds.isEmpty()) criteria.put("breedFactoryIds", breedFactoryIds);
            else return Response.ok(new Paging<>(0L,new ArrayList<>()));

            if(fromDate!=null) criteria.put("fromDate", fromDate);

            if(toDate != null) criteria.put("toDate",toDate);
            criteria.put("status", VeterinaryDrugsReportStatus.FINISHED.value());
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(veterinaryDrugsReportDao.pagingVeterinaryDrugsReports(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find VeterinaryDrugsReport fromDate={},fromDate={},toDate={}),cause:{}", fromDate,toDate,Throwables.getStackTraceAsString(e));
            return Response.fail("VeterinaryDrugsReport.find.fail");
        }
    }
    /**
     *根据时间起始 兽药方式查询
     */

    public Response<Paging<VeterinaryDrugsReport>> pagingbytimes (Integer pageNo, Integer pageSize, Map<String,Object> criteria){

        try{ PageInfo pageInfo = new PageInfo(pageNo, pageSize);

            return Response.ok(veterinaryDrugsReportDao.pagingbytimes(pageInfo.getOffset(), pageInfo.getLimit(),criteria));
        }catch(Exception e){
            log.error("failed to find pagingbytimes{}, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("pagingbytimes.find.fail");
        }

    }

    @Override
    public Response<Integer> countForWeb(Map<String, Object> map) {
        try{
            return Response.ok(veterinaryDrugsReportDao.countForWeb(map));
        }catch (Exception e){
            log.error("failed to count drug times ,case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to count drug times");
        }
    }


}
